package com.example.lcode.binary;

public class Question275 {

    public int hIndex(int[] citations) {
        return hIndex(citations, 0, citations.length - 1);
    }

    private int hIndex(int[] citations, int left, int right) {
        if (left > right) {
            return 0;
        }
        int mid = (left + right) / 2;
        if (citations[mid] >= (citations.length - mid) && (mid == 0 || citations[mid - 1] < (citations.length - mid + 1))) {
            return citations.length - mid;
        } else if (citations[mid] >= (citations.length - mid) && (mid != 0 && citations[mid - 1] >= (citations.length - mid + 1))) {
            return hIndex(citations, left, mid - 1);
        } else {
            return hIndex(citations, mid + 1, right);
        }
    }

    public static void main(String[] args) {
        int [] arr = {0,1,3,5,6};
        Question275 question275 = new Question275();
        int i = question275.hIndex(arr);
        System.out.println(i);
    }

}
